package com.tju.resource.business.tb_matl_store;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.mapper.TbMatlStoreMapper;
import com.tju.resource.model.TbMatlStore;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 */
@Service
public class TbMatlStoreServiceImpl extends ServiceImpl<TbMatlStoreMapper, TbMatlStore>
        implements TbMatlStoreService {
    @Autowired
    private ChineseExceptionUtils zh_CH;
    @Autowired
    private TbMatlStoreMapper matlStoreMapper;

    /**
     * 查询入库记录
     */
    @Override
    public Result<Object> selectTbMatlStore(ClientParameters param) {
        try {
            Integer pageNum = param.getPage();
            Integer limit = param.getLimit();
            if (EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(limit)) {
                Page<TbMatlStore> page = new Page<>(pageNum, limit);
                IPage<TbMatlStore> iPage = matlStoreMapper.selectTbMatlStore(page, param);
                return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }
    /**
     * 添加入库记录
     */
    @Override
    @Transactional
    public Result<Object> insertTbMatlStore(TbMatlStore matlStore) {
        if (matlStoreMapper.insert(matlStore) > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.AddedSuccessfully);
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.AddFailed);
    }
    /**
     * 删除入库记录
     */
    @Override
    @Transactional
    public Result<Object> deleteTbMatlStore(TbMatlStore matlStore) {
        if (EmptyUtils.isNotEmpty(matlStore.getId())) {
            if (matlStoreMapper.deleteById(matlStore.getId()) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyDeleted);
            }
        }

        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailedToDelete);
    }
}




